package com.glorysoft.knowlespda.ui.login.view

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseActivity
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.base.MyApp
import com.glorysoft.knowlespda.databinding.ActivityLoginBinding
import com.glorysoft.knowlespda.entity.ui.ToastMessageBean
import com.glorysoft.knowlespda.ui.home.view.HomeActivity
import com.glorysoft.knowlespda.ui.login.viewmodel.LoginViewModel
import com.glorysoft.knowlespda.ui.setting.PairingActivity
import com.glorysoft.knowlespda.ui.setting.SettingActivity
import com.glorysoft.knowlespda.utils.ChangeLanguageHelper
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import kotlinx.android.synthetic.main.activity_login.*
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions
import java.util.*

/**
 * FileName: LoginActivity
 * Author: XJH
 * Date: 2021/5/12 18:37
 * Description:
 * History:
 */
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() , EasyPermissions.PermissionCallbacks, View.OnClickListener {


    private var mSeverDialog: FreeUI_GeneralFragmentDialog? = null
    private var mLanguageDialog: FreeUI_GeneralFragmentDialog? = null

    override fun layoutId(): Int {
        return R.layout.activity_login
    }

    override fun initVariableId(): Int {
        return BR.loginViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        Constant.LANGUAGE = FreeApi_SharePreferencesUtils.getSharePre("KNOWLES_FILE", "LANGUAGE","中文", MyApp.app)
        when (Constant.LANGUAGE){
            "","中文" ->{
                ivTranslate.setImageResource(R.drawable.chinese)
            }
            "英文" ->{
                ivTranslate.setImageResource(R.drawable.english)
            }
        }
        viewModel.initUser(this)
        viewModel.defUI.startActivity.observe(this){
            startActivity(HomeActivity::class.java)
            finish()
        }

    }

    override fun initData() {
        btnLogin.setOnClickListener(this)
        ivSetting.setOnClickListener(this)
        tvSetting.setOnClickListener(this)
        ivTranslate.setOnClickListener(this)
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>?) {

    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("用户授权失败", false)
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms!!)) {
            AppSettingsDialog.Builder(this).build().show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.btnLogin ->{
                if (etOpID.text.toString().isNotEmpty()){
                    viewModel.login(etOpID.text.toString())
                }

            }
            R.id.tvSetting ->{
                getCustomDialog().show()
            }
            R.id.ivSetting -> startActivity(PairingActivity::class.java)
            R.id.ivTranslate ->{
                val language = FreeApi_SharePreferencesUtils.getSharePre("KNOWLES_FILE", "LANGUAGE","中文", MyApp.app)
                mLanguageDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(this,this.getString(R.string.confirm),
                    this.getString(R.string.cancel),this.getString(R.string.change_language_tip), {
                        when(language){
                            "中文" ->{
                                ChangeLanguageHelper.changeAppLanguage(this, Locale.SIMPLIFIED_CHINESE,"英文")
                            }
                            "英文" ->{
                                ChangeLanguageHelper.changeAppLanguage(this, Locale.ENGLISH,"中文")
                            }
                            else ->{
                                ChangeLanguageHelper.changeAppLanguage(this, Locale.SIMPLIFIED_CHINESE,"")
                            }
                        }
                        ChangeLanguageHelper.restart(this)
                    }
                ) { mLanguageDialog!!.dismiss() }
                mLanguageDialog?.show(this.supportFragmentManager, "LANGUAGE_DIALOG")
            }
        }
    }

    private fun getCustomDialog(): MaterialDialog {
        val dialog =  MaterialDialog(this)
            .cancelable(false)
            .cornerRadius(12f)
            .customView(R.layout.dialog_scan_confirm_cancel, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()
        val ivClose: ImageView = contentView.findViewById(R.id.ivClose)
        val edit: EditText = contentView.findViewById(R.id.edit)
        val tvConfirm: TextView = contentView.findViewById(R.id.tvConfirm)
        val tvCancel: TextView = contentView.findViewById(R.id.tvCancel)

        ivClose.setOnClickListener {
            dialog.dismiss()
        }

        tvCancel.setOnClickListener {
            dialog.dismiss()
        }


        tvConfirm.setOnClickListener {
            if (edit.text.toString() == "123456"){
                startActivity(SettingActivity::class.java)
                dialog.dismiss()
            }else{
                viewModel.defUI.toastEvent.value = ToastMessageBean("Password error",false)
            }

        }

        return dialog
    }
}